package com.base.myproject.client.HR.setting;


import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseRadioGroup;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.ui.Label;

public class Option extends BaseJDBCPanel {
	
	private BaseStore<BusiObject> store = null;
	private BaseStore<BusiObject> scheduleStore = null;
	private BaseComboBoxForm<BusiObject> bcbSalary = null;
	private Label label = null;
	private BaseRadioGroup attendance = null;
	private BaseRadioGroup batch;

	public Option(){
		init();
	}
	
	public void init(){
		initWidget();
		registerMainStore(store);
	}
	
	public void initWidget(){
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		vp.add(getSetExceptionForm());
		vp.add(getSetBatchOption());
		this.add(vp);
	}
	
	private BaseFormPanel getSetExceptionForm() {
		BaseFormPanel form = new BaseFormPanel(getStore(),false);
		TableLayout tl = new TableLayout(4);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("选项:"), td);
		String title[] = new String[]{"扣加班时长","扣款"};
		attendance = new BaseRadioGroup(title,true);
		attendance.addListener(Events.OnClick, getListener());
		attendance.setHeight(20);
		attendance.setName("code");
		form.add(attendance,td);
		
		label = new Label("请选择扣款的薪金项目:");
		form.add(label,td);
		bcbSalary = new BaseComboBoxForm<BusiObject>("code", "desc_cn","hr_Salary_Item" ,"where code like 'D%25'",true);
		bcbSalary.setName("mark");
		form.add(bcbSalary,td);
		showSarlyItem(false);
		form.inited();
		
		return form;
	}
	
	private BaseFormPanel getSetBatchOption() {
		BaseFormPanel form = new BaseFormPanel(getScheduleStore(),false);
		TableLayout tl = new TableLayout(4);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("是否排班:"), td);
		String title[] = new String[]{"是","否"};
		batch = new BaseRadioGroup(title,true);
		batch.addListener(Events.OnClick, getListener());
		batch.setHeight(20);
		batch.setName("batchNo");
		form.add(batch,td);
		
		form.add(new Label(""), td);
		
		Button save = new Button("保存");
		save.addListener(Events.Select, getListener());
		form.add(save, td);
		
		time1.setName("bgnTime1");
		time2.setName("endTime1");
		time3.setName("bgnTime2");
		time4.setName("endTime2");
		
		time1.setWidth(100);
		time2.setWidth(100);
		time3.setWidth(100);
		time4.setWidth(100);
		
		form.add(time1, td);
		form.add(time2, td);
		form.add(time3, td);
		form.add(time4, td);
		
		form.inited();
		
		return form;
	}
	
	TextField<String> time1 = new TextField<String>();
	TextField<String> time2 = new TextField<String>();
	TextField<String> time3 = new TextField<String>();
	TextField<String> time4 = new TextField<String>();
	
	private void isShowTimes(boolean visible) {
		time1.setVisible(visible);
		time2.setVisible(visible);
		time3.setVisible(visible);
		time4.setVisible(visible);
	}
	
	public BaseStore<BusiObject> getStore(){
		if(store != null){
			return store;
		}else{
			createStore();
		}
		return store;
	}
	
	public BaseStore<BusiObject> createStore(){
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");

		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(Integer.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("dictionary");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode,name,type,substring(code,1,1) as code,mark from dictionary where type='option_attendance'");
		store = new BaseStore<BusiObject>(type, info){
			@Override
			public void _LoadComplete() {
				super._LoadComplete();
				if(store.getValue("code").toString().equals("1")){
					showSarlyItem(false);
				}else{
					showSarlyItem(true);
				}
			}
		};
		return store;
	}
	
	public BaseStore<BusiObject> getScheduleStore(){
		if(scheduleStore != null){
			return scheduleStore;
		}else{
			createScheduleStore();
		}
		return scheduleStore;
	}
	
	public BaseStore<BusiObject> createScheduleStore(){
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");

		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("batchNo");
		type.getField("batchNo").setType(Integer.class);
		type.addField("bgnTime1");
		type.getField("bgnTime1").setType(String.class);
		type.addField("bgnTime2");
		type.getField("bgnTime2").setType(String.class);
		type.addField("endTime1");
		type.getField("endTime1").setType(String.class);
		type.addField("endTime2");
		type.getField("endTime2").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_WorkBatch");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode, bgnTime1, bgnTime2, endTime1, endTime2, substring(batchNo, 1, 1) as batchNo from hr_Attendance_WorkBatch where depCode is null");
		scheduleStore = new BaseStore<BusiObject>(type, info){
			@Override
			public void _LoadComplete() {
				super._LoadComplete();
				if(scheduleStore.getValue("batchNo").toString().indexOf("1") != -1){
					isShowTimes(false);
				} else {
					isShowTimes(true);
				}
			}
		};
		return scheduleStore;
	}
	
	public void showSarlyItem(boolean isShow){
		label.setVisible(isShow);
		bcbSalary.setVisible(isShow);
	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;
	private Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(attendance)) {
								for(int i = 0;i < attendance.getValueArray().length;i++){
									if(attendance.getValueArray()[i].equals("扣款")){
										showSarlyItem(true);
									}else{
										showSarlyItem(false);
									}
								}
							} else {
								for(int i = 0;i < batch.getValueArray().length;i++){
									if(batch.getValueArray()[i].equals("否")){
										isShowTimes(true);
									}else{
										isShowTimes(false);
									}
								}
							}
						} else {
							scheduleStore.trySave();
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
